package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.entry.SysDict;
import com.itheima.ydd.entry.SysMenu;
import com.itheima.ydd.entry.SysUser;
import com.itheima.ydd.mapper.SysDictMapper;
import com.itheima.ydd.service.SysDictService;
import com.itheima.ydd.service.SysMenuService;
import com.itheima.ydd.service.SysUserService;
import com.itheima.ydd.vo.ProfileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserService sysUserService;


    public ProfileVo profile(String sysUserId){
        ProfileVo profileVo = new ProfileVo();
        //本次登录的用户
        SysUser sysUser = sysUserService.getById(sysUserId);
        profileVo.setUserInfo(sysUser);
        LambdaQueryWrapper<SysMenu> menuLqw = new LambdaQueryWrapper();
        menuLqw.eq(SysMenu::getType,1);
        List<SysMenu> list = sysMenuService.list(menuLqw);
        profileVo.setMenuList(list);
        //
        QueryWrapper<SysDict> dictqw = new QueryWrapper<>();
        dictqw.select("DISTINCT type");
        List<SysDict> types = this.list(dictqw); //5条数据 不同type
        Map<String,Map<String,String >> dictList = new HashMap<>();

        for (SysDict type : types) {
            Map<String,String> dictMap = new HashMap<>();
            LambdaQueryWrapper<SysDict> dictLqw = new LambdaQueryWrapper<>();
            dictLqw.eq(SysDict::getType,type.getType());
            List<SysDict> dicts = this.list(dictLqw);
            dicts.forEach(dict->{
                dictMap.put(dict.getValue(),dict.getLabel());
            });
            dictList.put(type.getType(),dictMap);
        }
        profileVo.setDictsList(dictList);
        return profileVo;
    }
}
